const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//兑换码记录id
    active: 0,
    show: false,
    code: "",
    code_num: "",
    dialog_type: 0,
    nickname:"",
    uid:"",
    name:"",
    show_tips:false,
    timer_id: null,
    keyword:"",
    code_sn:"",
    type:"binding",
    id:"",

    list: [],
    current_page: 1,
    last_page: 1,

    get_time_sort: 'asc',
    active_end_sort: 'asc',
    show_member_flag:false,
    member_list:[],

  },
  behaviors: [skinBehavior],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({get_time_sort:'desc'})
    // this.getData({
    //   page: 1,order:{get_time:'desc'}
    // })
  },
  getData(json) {
    app._postNetWork({
      url: app.getNetAddresss('plugin.gift-card.api.record.list'),
      data: json,
      success: resdata => {
        let {
          result,
          data: {
            data,
            current_page,
            last_page
          },
          msg
        } = resdata.data;
        if (result) {
          this.setData({
            current_page,
            last_page
          })

          if (current_page == 1) {
            this.setData({
              list: data || []
            })
          } else {
            this.setData({
              list: this.data.list.concat(data)
            })
          }
        } else {
          app.tips(msg)
        }
      }
    })
  },
  toHistory() {
    wx.navigateTo({
      url: '/packageG/gift_card/history/history?status=1'
    })
  },

  onShareAppMessage: function (res) {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    console.log(res)
    if (res?.from == "button") {
      const { code_sn, code_id, id, face_value, name } = res.target.dataset.datas
      const { host } = app.data
      //o:操作  1：激活 2:绑定 3:兑换 4:转赠 
      //o=4&mid=3&code_id=24&record_id=775&code_sn=YHK2Mznr8ol1
      var path= "/packageG/gift_card/index/index?option_type=4&record_id=" + id +"&mid=" + mid  + "&code_sn=" + code_sn + "&code_id=" + code_id + '&face_value=' + face_value;
      // console.log("path = : ",path)
      return {
        title: `【面值：${face_value}元】${name}`,
        path:  path,
        imageUrl: host + "/static/images/gift-card-share.png"
      };  
    }
  },
  showOverlay(e) {
    if(e.currentTarget.dataset.type == "1"){
      this.setData({
        type:"Donation",
        code_sn: e.currentTarget.dataset.datas.code_sn,
        code_id:e.currentTarget.dataset.datas.code_id,
        id:e.currentTarget.dataset.datas.id
      })
    }else{
      this.setData({binding:"binding"})
    }
    this.setData({
      show: true,
      dialog_type: e.currentTarget.dataset.type,
    })
  },
  link(e) {
    let {code_sn,code_id,face_value,id,code_num} = e.currentTarget.dataset.datas;
    // const tipText = `亲，绑定到您的账号后将无法做解绑操作。卡号：${code_num}面额：${face_value}元`
    // wx.showModal({
    //   title: '确认绑定礼品卡',
    //   content: tipText,
    //   complete: (res) => {
    //     if (res.confirm) {
          
    //     }
    //   }
    // })
    wx.navigateTo({
      url: '/packageG/gift_card/index/index?code_id='+code_id+'&code_sn='+code_sn+'&face_value='+face_value+'&record_id='+id,
    })
  },
  HideOverlay() {
    this.setData({
      show: false
    })
  },
  input_handle(evt) {
    let type = evt.currentTarget.dataset.type;
    let value = evt.detail.value;

    if(type == 'code'){
      this.setData({code:value,type:'binding'})

    }else if (type == 'codeNum') {
      this.setData({code_num:value,type:'binding'})
    }else{
      this.setData({keyword:value,type:"presented"})
      if (this.data.timer_id) {
        clearTimeout(this.data.timer_id);
      }
      this.data.timer_id = setTimeout(() => {
        this.getMemberInfo(value)
      }, 600);
    }
  },
  confirm() {
    if (this.data.type == 'binding') {
      const { code_num, code } = this.data;
      if (!code_num) {
        app.tips('请输入卡号');
        return;
      }
      if (!code) {
        app.tips('请输入卡密');
        return;
      }
      app._getNetWork({
        url:app.getNetAddresss('plugin.gift-card.api.record.bind'),
        data:{
          code_sn:this.data.code,
          code_num
        },
        success:resdata=>{
          let { result, msg }  = resdata.data;
          app.tips(msg);
          if(result){
            this.setData({show:false});
            let json = {};
            let index = this.data.active;
            if (index == 2 || index == 3) {
              json.search = {
                type: index - 1
              }
            }
            if (index == 0 || index == 1) {
              if (index == 0) {
                json.order = {
                  get_time: 'desc'
                };

              } else {
                json.order = {
                  active_end:'desc'
                };

              }
            }
            this.getData(json);
          }
        }
      })
    } else {
      app._getNetWork({
        url:app.getNetAddresss('plugin.gift-card.api.record.tranfer'),
        data:{
          uid:this.data.uid,
          id:this.data.id
        },
        success:resdata=>{
          let { result, msg ,data:{title,imgUrl,desc}}  = resdata.data;
          app.tips(msg);
          if(result){

            wx.setStorageSync("wxShareInfo",{title,imgUrl,description:desc})
            setTimeout(()=>{
              wx.navigateTo({
                url: '/packageI/redeem_code/index/index?share=1&code_id='+this.data.code_id+'&sn_code='+this.data.code_sn,
              })
            },300)
          }
        }
      })
    }
  },
  getMemberInfo(keyword) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.gift-card.api.record.memberSearch'),
      data: {
        keyword
      },
      success: resdata => {
        let {
          result,
          msg,
          data: {
            data
          }
        } = resdata.data;
        if (result) {
          if (data && data.length !== 0) {
            this.setData({
              member_list:data,
              show_member_flag:true,
              show_tips:false
            })
          }
          if (data.length == 0) {
            this.setData({show_tips:true,member_list:[],show_member_flag:false})
          }
        } else {
          app.tips(msg)
        }
      }
    })

  },
  choose_handle(e){
    let {uid,mobile,nickname,name} =  e.currentTarget.dataset.item;
    this.setData({
      uid,
      nickname,
      name,
      keyword:mobile||uid,
      show_member_flag:false
    })
  },
  onChange(event) {
    let index = event.detail.index;
    let json = this.get_tab_params(index);
    console.log(json,'json');
    this.getData(json);
    this.setData({
      active: event.detail.index
    })
  },
  get_tab_params(index) {
    let json = {};
    if (index == 2 || index == 3) {
      json.search = {
        type: index - 1
      }
    }
    if (index == 0 || index == 1) {
      if (index == 0) {

        if (this.data.get_time_sort == 'asc') {
          this.data.get_time_sort = 'desc'
        } else {
          this.data.get_time_sort = 'asc'
        }
        json.order = {
          get_time: this.data.get_time_sort
        };
      } else {

        if (this.data.active_end_sort == 'asc') {
          this.data.active_end_sort = 'desc'
        } else {
          this.data.active_end_sort = 'asc'
        }
        json.order = {
          active_end: this.data.active_end_sort
        };
      }
    }
    return json;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let json = {};
    let index = this.data.active;
    if (index == 2 || index == 3) {
      json.search = {
        type: index - 1
      }
    }
    if (index == 0 || index == 1) {
      if (index == 0) {
        json.order = {
          get_time: this.data.get_time_sort
        };
      } else {
        json.order = {
          active_end: this.data.active_end_sort
        };
      }
    }
    this.getData(json)
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let json = {};
    let index = this.data.active;
    if (index == 2 || index == 3) {
      json.search = {
        type: index - 1
      }
    }
    if (index == 0 || index == 1) {
      if (index == 0) {
        json.order = {
          get_time: this.data.get_time_sort
        };
      } else {
        json.order = {
          active_end: this.data.active_end_sort
        };
      }
    }
    this.getData({
      page: this.data.current_page + 1,
      ...json
    });
  },

})